package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToggleButton;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.Circle_ImageView;
import com.jaya.budan.views.CommonPopWindow;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Chat_Msg_Set_Activity extends BaseActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {

    private RelativeLayout back, rl_about_us_pingjia, rl_pop_geren_szbz, rl_msg_set_weidu, rl_msg_set_liebiao;
    private String access_token;
    private Circle_ImageView iv_chat_set_icon;
    private ImageView iv_chat_set_is_vip;
    private TextView tv_chat_set_name;
    private User_Limit user_limit;
    private int position;
    private ToggleButton tb_mes_tx_1, tb_mes_tx_2;
    public static Activity activity;
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        setContentView(R.layout.activity_chat_message_set);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        position = intent.getIntExtra("position", 0);
        user_limit = (User_Limit) intent.getSerializableExtra("user_limit");
        back = findViewById(R.id.back);
        rl_about_us_pingjia = findViewById(R.id.rl_about_us_pingjia);
        rl_pop_geren_szbz = findViewById(R.id.rl_pop_geren_szbz);
        rl_msg_set_weidu = findViewById(R.id.rl_msg_set_weidu);
        rl_msg_set_liebiao = findViewById(R.id.rl_msg_set_liebiao);
        iv_chat_set_icon = findViewById(R.id.iv_chat_set_icon);
        iv_chat_set_is_vip = findViewById(R.id.iv_chat_set_is_vip);
        tv_chat_set_name = findViewById(R.id.tv_chat_set_name);
        tb_mes_tx_1 = findViewById(R.id.tb_mes_tx_1);
        tb_mes_tx_2 = findViewById(R.id.tb_mes_tx_2);
        back.setOnClickListener(this);
        rl_about_us_pingjia.setOnClickListener(this);
        rl_msg_set_weidu.setOnClickListener(this);
        rl_pop_geren_szbz.setOnClickListener(this);
        rl_msg_set_liebiao.setOnClickListener(this);
        tv_chat_set_name.setText("" + user_limit.getNicename());
        Glide.with(this).load(user_limit.getHead()).apply(new RequestOptions()
                .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
        ).into(iv_chat_set_icon);
        if (user_limit.getIs_vip() == 1){
            iv_chat_set_is_vip.setVisibility(View.VISIBLE);//是 会员
            tv_chat_set_name.setTextColor(Color.parseColor("#FF3B30"));
        } else {
            iv_chat_set_is_vip.setVisibility(View.GONE);
            tv_chat_set_name.setTextColor(Color.parseColor("#000000"));
        }
        tb_mes_tx_1.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    ToolUtils.showToast(Chat_Msg_Set_Activity.this, "聊天已置顶");
                } else {
                    ToolUtils.showToast(Chat_Msg_Set_Activity.this, "聊天已关闭");
                }
            }
        });
        tb_mes_tx_2.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    ToolUtils.showToast(Chat_Msg_Set_Activity.this, "消息免打扰已开启");
                } else {
                    ToolUtils.showToast(Chat_Msg_Set_Activity.this, "消息免打扰已关闭");
                }
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_about_us_pingjia:
                JSONObject object = new JSONObject();
                try {
                    object.put("uid", user_limit.getUid());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postSetBlackUserData(object);
                break;
            case R.id.rl_pop_geren_szbz:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_update_msg_name)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(Chat_Msg_Set_Activity.this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(Chat_Msg_Set_Activity.this)
                        .showAsCenter(v);
                break;
            case R.id.rl_msg_set_weidu:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_fans_cancle_2)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(Chat_Msg_Set_Activity.this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(Chat_Msg_Set_Activity.this)
                        .showAsCenter(v);
                break;
            case R.id.rl_msg_set_liebiao:
                Intent intent = new Intent(Chat_Msg_Set_Activity.this, TaRen_Set_Report_Activity.class);
                intent.putExtra("access_token", access_token);
                intent.putExtra("uid", user_limit.getUid() + "");
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postSetFansBZData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.lt_set_fans_bz_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai_set", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        ToolUtils.showToast(Chat_Msg_Set_Activity.this, message);
                    } else {
                        ToolUtils.showToast(Chat_Msg_Set_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Chat_Msg_Set_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postSetBlackUserData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.tr_user_information_black_user_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai_black", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        ToolUtils.showToast(Chat_Msg_Set_Activity.this, message);//被拉黑
                    } else {
                        ToolUtils.showToast(Chat_Msg_Set_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Chat_Msg_Set_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_geren_update_msg_name:
                EditText tv_geren_fans_cancle1 = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView tv_cancle1 = view.findViewById(R.id.tv_cancle);
                TextView tv_pop_update_msg_name_title = view.findViewById(R.id.tv_pop_update_msg_name_title);
                TextView img_guanbi1 = view.findViewById(R.id.img_guanbi);
                TextView tv_pop_geren_update_name_count = view.findViewById(R.id.tv_pop_geren_update_name_count);
                tv_pop_update_msg_name_title.setText("备注");
                tv_geren_fans_cancle1.setHint("请输入备注");
                tv_cancle1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        String s = tv_geren_fans_cancle1.getText().toString().trim();
                        if (ToolUtils.isEmpty(s)){
                            ToolUtils.showToast(Chat_Msg_Set_Activity.this, "备注名不能为空");
                            return;
                        } else {
                            ToolUtils.showToast(Chat_Msg_Set_Activity.this, "" + s);
                            JSONObject object = new JSONObject();
                            try {
                                object.put("t_uid", user_limit.getUid());
                                object.put("name", s);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            postSetFansBZData(object);
                        }
                    }
                });
                tv_geren_fans_cancle1.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void afterTextChanged(Editable s) {
                        tv_pop_geren_update_name_count.setText(s.length() + "/20");
                    }
                });
                break;
            case R.layout.pop_geren_fans_cancle_2:
                TextView tv_cancle = view.findViewById(R.id.tv_cancle);
                TextView tv_geren_fans_cancle = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView img_guanbi = view.findViewById(R.id.img_guanbi);
                tv_geren_fans_cancle.setText("确认清空与他的聊天记录吗？\n" +
                        "（删除后聊天记录不可恢复）");
                tv_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                break;
            default:
                break;
        }
    }
}
